package com.leyou.trade.web;

import com.leyou.trade.entity.CartItem;
import com.leyou.trade.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("cart")
@RestController
public class CartController {
    @Autowired
    CartService cartService;
    /**
     * 新增购物车
     * @param cartItem  数据
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveCartItem(@RequestBody CartItem cartItem){
        cartService.saveCartItem(cartItem);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //查询购物车
    @GetMapping("list")
    public ResponseEntity<List<CartItem>> queryCartList(){
        return ResponseEntity.ok(cartService.queryCartList());
    }
    //修改商品
    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("id") Long skuId,
                                          @RequestParam("num") Integer num){
    cartService.updateNum(skuId, num);
        return ResponseEntity.noContent().build();
    }
    //删除购物车
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId){
        cartService.deleteCart(skuId);
        return ResponseEntity.noContent().build();
    }
    //批量新增
    @PostMapping("list")
    public  ResponseEntity<Void> addCartItemList(@RequestBody List<CartItem> items){
        cartService.addCartItemList(items);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
